package com.bluefeather.songlyric;

import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;

public class SongLyricActivity extends Activity implements
		Drawer.NavigationDrawerCallbacks {
	
	private static String 		TAG = "ACTIVITY";
	
	private Drawer				_drawer;
	private CharSequence 		_title;	// Used to store the last screen title. For use in {@link #restoreActionBar()}
	private boolean				_initialized = false;
	private MusicList			_musicList = null;
	private ToneList			_toneList = null;
	private int[]				_chinesePages = new int[] {
		R.string.chinese_1, R.string.chinese_2, R.string.chinese_3, R.string.chinese_4, R.string.chinese_5,
		R.string.chinese_6, R.string.chinese_7, R.string.chinese_8, R.string.chinese_9, R.string.chinese_10,
		R.string.chinese_11, R.string.chinese_12, R.string.chinese_13, R.string.chinese_14, R.string.chinese_15
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app);
		_drawer = (Drawer) getFragmentManager().findFragmentById(R.id.navigation_drawer);
		_title = getTitle();
		// Set up the drawer.
		_drawer.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
		_initialized = true;
	}

	@Override
	public void onNavigationDrawerItemSelected(int position) {
		Fragment fragment = null;
		if(!_initialized) {
			// display cover as the default page
			fragment = new Cover();
		} else {
			int menuId = Drawer._menuItems[position];
			_title = getString(menuId);
			if(position <= 7) {
				_toneList = null;
				_musicList = MusicList.newInstance(menuId);
				fragment = _musicList;
			} else if(position <= 9) {
				_musicList = null;
				_toneList = ToneList.newInstance(menuId);
				fragment = _toneList;
			} else {
				_toneList = null;
				_musicList = null;
				fragment = AuthorList.newInstance();
			}
		}
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
	}
	
	public void setPageInTitle(int i) {
		_title = getString(R.string.roll) + getString(_chinesePages[i]);
		getActionBar().setTitle(_title);
	}

	public void restoreActionBar() {
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(_title);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if(_musicList != null)
			getMenuInflater().inflate(R.menu.lyric, menu);
		else if(_toneList != null)
			getMenuInflater().inflate(R.menu.tone, menu);
		else
			getMenuInflater().inflate(R.menu.global, menu);
		restoreActionBar();
		return true;
	}
	
	public void searchMusic(String name) {
		if(_musicList != null)
			_musicList.searchMusic(name);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_about) {
			Intent i = new Intent(this, DocActivity.class);
			i.putExtra("DOC", "about");
			startActivity(i);
			return true;
		} else if(id == R.id.action_settings) {
			Intent i = new Intent(this, SettingsActivity.class);
			startActivity(i);
			return true;
		} else if(id == R.id.action_search) {
			if(_musicList != null) {
				SearchMusicDlg dlg = new SearchMusicDlg(this);
				dlg.show();
			} else if(_toneList != null) {
				SearchToneDlg dlg = new SearchToneDlg(this);
				dlg.show();
			}
			return true;
		} else if(id == R.id.action_go_prev) {
			if(_musicList != null)
				_musicList.goPrev();
			return true;
		} else if(id == R.id.action_go_next) {
			if(_musicList != null)
				_musicList.goNext();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onBackPressed() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.warning_exit).setCancelable(false).setPositiveButton("YES",
			new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			
		}).setNegativeButton("NO",
			new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public void openMusic(int musicId) {
		LogDog.i(TAG, "openMusic " + musicId);
		DB.instance().importMusic(musicId);
		Intent intent = new Intent(this, MusicActivity.class);
		intent.putExtra("LYRIC_WHERE", DB.LYRIC._MUSIC + " = " + musicId);
		startActivity(intent);
	}
	
	public void openTone(int toneIndex, int toneType) {
		LogDog.i(TAG, "openTone " + toneIndex + "," + toneType);
		Intent intent = new Intent(this, ToneActivity.class);
		intent.putExtra("TONE_INDEX", toneIndex);
		intent.putExtra("TONE_TYPE", toneType);
		startActivity(intent);
	}

	public void openAuthor(String author) {
		Log.i(TAG, "openAuthor " + author);
		Intent intent = new Intent(this, MusicActivity.class);
		intent.putExtra("LYRIC_WHERE", DB.LYRIC._AUTHOR + " = '" + author + "'");
		startActivity(intent);
	}

}
